package org.keyroy.map_editor.map.m;

import it.sauronsoftware.base64.Base64;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;

import org.keyroy.map_editor.map.Map;
import org.keyroy.map_editor.map.bin.CoordinateBin;
import org.keyroy.tag.Tag;
import org.keyroy.tag.XmlSupport;

public class Building implements XmlSupport, DrawableShape {
	public int ID;

	public File file;
	public boolean[][] collides;

	public boolean isHide;
	public BufferedImage bufferedImage;
	public PixRect pixRect = new PixRect();
	public MapRect mapRect = new MapRect();;

	public Building() {
	}

	public Building(Building building) {
		this.ID = building.ID;
		this.file = building.file;
		this.collides = building.collides;
		this.bufferedImage = building.bufferedImage;

		mapRect = new MapRect();
		pixRect = new PixRect();

		mapRect.set(building.mapRect);
		pixRect.set(building.pixRect);

	}

	public Building(int ID, File source, Map map, BufferedImage bufferedImage) {
		this.ID = ID;
		this.file = source;
		init(map, bufferedImage);
	}

	public Building getCopy() {
		return new Building(this);
	}

	public void init(Map map, BufferedImage bufferedImage) {
		this.bufferedImage = bufferedImage;
		pixRect.setSize(bufferedImage.getWidth(), bufferedImage.getHeight());
		CoordinateBin.pSize2mSize(map, pixRect, mapRect);
		CoordinateBin.mSize2pSize(map, mapRect, pixRect);
	}

	public void setLocation(Map map, Point mapPoint) {
		mapRect.setLocation(mapPoint.x, mapPoint.y);
		CoordinateBin.map2pix(map, mapRect, pixRect);
	}

	@Override
	public void drawShape(Graphics g) {
		if (bufferedImage != null && isHide == false) {
			g.drawImage(bufferedImage, pixRect.x + (pixRect.width - bufferedImage.getWidth()), pixRect.y
					+ (pixRect.height - bufferedImage.getHeight()), null);
			pixRect.drawShape(g);
		}
	}

	@Override
	public void fillShape(Graphics g) {
		drawShape(g);
	}

	@Override
	public String getTagName() {
		return "b";
	}

	@Override
	public void initObj(Tag tag) {
		ID = tag.getParameterIntValue("id");
		file = new File(tag.getParameterValue("s"));
		mapRect.set(tag.getParameterIntValue("w"), tag.getParameterIntValue("h"));
		String c = tag.getParameterValue("c");
		if (c != null) {
			byte[] bs = Base64.decode(c).getBytes();
			collides = new boolean[mapRect.width][mapRect.height];
			ByteArrayInputStream inputStream = new ByteArrayInputStream(bs);
			for (int i = 0; i < mapRect.height; i++) {
				for (int j = 0; j < mapRect.width; j++) {
					try {
						collides[i][j] = inputStream.read() == 1;
					} catch (Exception e) {
					}
				}
			}
		}

	}

	@Override
	public void initTag(Tag tag) {
		tag.addParameter("id", ID);
		tag.addParameter("s", file.getAbsolutePath());
		tag.addParameter("w", mapRect.width);
		tag.addParameter("h", mapRect.height);
		if (collides != null) {
			ByteArrayOutputStream outputStream = new ByteArrayOutputStream(mapRect.width * mapRect.height);
			for (int i = 0; i < mapRect.height; i++) {
				for (int j = 0; j < mapRect.width; j++) {
					if (collides[i][j] == false) {
						outputStream.write(0);
					} else {
						outputStream.write(1);
					}
				}
			}
			String c = new String(Base64.encode(outputStream.toByteArray()));
			tag.addParameter("c", c);
		}
	}

}
